{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "0a8098a1-8e77-4c55-bdaf-5551e0f303b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "信计221 刘显婷 224180117\n",
      "        电影名     累计票房                 导演            上映时间        类型\n",
      "0      让子弹飞  65355.3       姜文 Wen Jiang  2010-12-16（中国）     喜剧/动作\n",
      "1     唐山大地震  65015.7  冯小刚 Xiaogang Feng   2010-7-22（中国）  剧情/亲情/灾难\n",
      "2     非诚勿扰2  47159.2  冯小刚 Xiaogang Feng  2010-12-22（中国）     爱情/喜剧\n",
      "3  狄仁杰之通天帝国  28620.3       徐克 Hark Tsui   2010-9-29（中国）  动作/悬疑/古装\n",
      "4  叶问2：宗师传奇  23110.5     叶伟信 Wilson Yip   2010-4-27（中国）     动作/剧情\n"
     ]
    }
   ],
   "source": [
    "print(\"信计221 刘显婷 224180117\")\n",
    "import pandas as pd\n",
    "from pyecharts.charts import Line, Pie, Bar\n",
    "from pyecharts import options as opts\n",
    "#读取数据\n",
    "df = pd.read_csv('T2_movies.csv')\n",
    "#删除电影名中的时间信息，处理累计票房数据\n",
    "df['电影名'] = df['电影名'].str.replace(r'[（\\(]\\d{4}[）\\)]', '', regex=True)\n",
    "df['累计票房'] = df['累计票房'].str.replace('累计票房', '').str.replace('万', '').astype(float)\n",
    "print(df.head(5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "3e4c7f89-551a-422b-af28-7993992a26a4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        电影名     累计票房   导演            上映时间  类型\n",
      "0      让子弹飞  65355.3   姜文  2010-12-16（中国）  喜剧\n",
      "1     唐山大地震  65015.7  冯小刚   2010-7-22（中国）  剧情\n",
      "2     非诚勿扰2  47159.2  冯小刚  2010-12-22（中国）  爱情\n",
      "3  狄仁杰之通天帝国  28620.3   徐克   2010-9-29（中国）  动作\n",
      "4  叶问2：宗师传奇  23110.5  叶伟信   2010-4-27（中国）  动作\n"
     ]
    }
   ],
   "source": [
    "#处理导演和类型数据\n",
    "df['导演'] = df['导演'].str.split().str[0]\n",
    "df['类型'] = df['类型'].str.split('/').str[0]\n",
    "print(df.head(5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f65c011b-edc8-4cd3-b428-ad868b4e6dd1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        电影名     累计票房   导演  上映时间  类型\n",
      "0      让子弹飞  65355.3   姜文  2010  喜剧\n",
      "1     唐山大地震  65015.7  冯小刚  2010  剧情\n",
      "2     非诚勿扰2  47159.2  冯小刚  2010  爱情\n",
      "3  狄仁杰之通天帝国  28620.3   徐克  2010  动作\n",
      "4  叶问2：宗师传奇  23110.5  叶伟信  2010  动作\n"
     ]
    }
   ],
   "source": [
    "#处理上映时间数据\n",
    "df['上映时间'] = df['上映时间'].str.split('-').str[0]\n",
    "print(df.head(5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b1bc8ec3-9208-432e-a7f4-95dc04de0b8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "#保存处理好的数据\n",
    "df.to_csv('movies_清洗后.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "7acac3d6-0f9a-4421-ac85-19b5ad884a4d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'C:\\\\Users\\\\19202\\\\每年上映电影数量变化趋势.html'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2. 数据可视化\n",
    "#使用折线图展示每年上映的电影数量变化趋势\n",
    "movie_count_by_year = df['上映时间'].value_counts().sort_index()\n",
    "line = Line()\n",
    "line.add_xaxis(movie_count_by_year.index.tolist())\n",
    "line.add_yaxis('每年上映电影数量', movie_count_by_year.values.tolist())\n",
    "line.set_global_opts(\n",
    "    title_opts=opts.TitleOpts(title='每年上映电影数量', title_textstyle_opts=opts.TextStyleOpts(color='red', font_size=20), pos_left='center'),\n",
    "    tooltip_opts=opts.TooltipOpts()\n",
    ")\n",
    "line.render('每年上映电影数量变化趋势.html')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "f4a731af-3f3a-407d-b9a3-f812f1099f29",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'C:\\\\Users\\\\19202\\\\各类型电影数量占比.html'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#使用环形图展示各类型电影数量占比情况\n",
    "type_count = df['类型'].value_counts()\n",
    "pie = Pie()\n",
    "pie.add('', [list(z) for z in zip(type_count.index.tolist(), type_count.values.tolist())], radius=['50%', '75%'])\n",
    "pie.set_global_opts(title_opts=opts.TitleOpts(title='各类型电影数量占比'))\n",
    "pie.set_series_opts(label_opts=opts.LabelOpts(formatter='{b}: {c}'))\n",
    "pie.render('各类型电影数量占比.html')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "3889411f-8675-4312-9947-2840ab3d6102",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'C:\\\\Users\\\\19202\\\\票房TOP5电影名称.html'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#使用柱状图展示票房最高的前5项电影\n",
    "top5_movies = df.sort_values(by='累计票房', ascending=False).head(5)\n",
    "bar = Bar()\n",
    "bar.add_xaxis(top5_movies['电影名'].tolist())\n",
    "bar.add_yaxis('票房（万）', top5_movies['累计票房'].tolist())\n",
    "bar.set_global_opts(title_opts=opts.TitleOpts(title='票房TOP5电影名称'))\n",
    "bar.render('票房TOP5电影名称.html')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5ec783f5-35cb-4239-9d86-9267e81604d3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
